﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using CNdotnet.Ezsocio.DataProvider;
using CNdotnet.Ezsocio.Core.Repository;
using CNdotnet.Ezsocio.DataEntities;

#endregion

namespace CNdotnet.Ezsocio.SQLServerProvider
{
    public class FriendGroupRepository : RepositoryBase,IFriendGroupRepository
    {
        #region Need refacting       
       
        public void Delete(int id) {
            using (RepositoryContext db = new RepositoryContext()) {
                var fg = (from friendGroup in db.GetTable<FriendGroup>()
                          where friendGroup.ID == id
                          select friendGroup).SingleOrDefault<FriendGroup>();
                if (fg != null) {
                    db.GetTable<FriendGroup>().DeleteOnSubmit(fg);
                    db.SubmitChanges();
                }
            }
        }

        public void Update(FriendGroup friendGroup)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var fg = (from u in db.GetTable<FriendGroup>()
                          where u.ID == friendGroup.ID
                         select u).SingleOrDefault<FriendGroup>();
                if (fg != null)
                {
                    fg.GroupName = friendGroup.GroupName;
                    db.SubmitChanges();
                }
            }
        }
        #endregion 
     
        public FriendGroup Insert(FriendGroup group)
        {
            InsertEntity(group);
            return group;
        }

        public FriendGroup GetSingle(int groupId)
        {
            FriendGroup group = GetEntity<FriendGroup>(g => g.ID == groupId);
            return group;
        }
    }
}
